// pages/share/share.js
import { ajax, src, util } from "../../utils/util.js";

const app = getApp()
let isIphoneX = app.globalData.isIphoneX;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    uqid:'',
    shareurl:'',
    timestart:'',
    timeend:'',
    isShow: false,

    isIphoneX: isIphoneX
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uqid = wx.getStorageSync("uqid");
    this.setData({
      uqid: uqid
    });
    this.myShare(uqid);
  },
  
  myShare: function (uqid) {
    ajax("/api/index/get_unlimit", {
      data: {
        uqid: uqid
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method:'POST'
    }, res => {
      // console.log(res);
      if (res.error == 0) {
        this.setData({
          shareurl: res.data.image
        })
        
      }
    })
  },

  //点击开始的时间  
  timestart: function (e) {
    var _this = this;
    _this.setData({ timestart: e.timeStamp });
  },
  //点击结束的时间
  timeend: function (e) {
    var _this = this;
    _this.setData({ timeend: e.timeStamp });
  },

  //保存图片
  saveImg: function (e) {
    var _this = this;
    var times = _this.data.timeend - _this.data.timestart;
    // 单击预览 长按保存
    wx.previewImage({
      current: this.data.shareurl, 
      urls: [this.data.shareurl] 
    })
   
  },
  handleDown:function(){
    var that = this;
    console.log(that.data.shareurl)
    wx.showLoading({
      title: '保存中'
    })
    wx.downloadFile({
      url: that.data.shareurl, // 仅为示例，并非真实的资源
      success(res) {
        console.log(res)
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          let path = res.tempFilePath
          wx.saveImageToPhotosAlbum({
            filePath: path,
            success: (res) => {
              wx.hideLoading();

              wx.showToast({
                title: '已保存至您的相册',
                icon: 'none',
                duration: 2000
              })

            },
            fail: function (err) {
              wx.hideLoading();
              if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                that.setData({
                  isShow: true
                })
              } else if (err.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
                that.setData({
                  isShow: false
                })
                wx.showToast({
                  title: '取消保存',
                  icon: 'none',
                  duration: 2000
                })
              } else if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied') {
                that.setData({
                  isShow: true
                })
              }

            }
          })

        }
      }
    })
  },
  handleSetting(e) {
    var that = this
    if (!e.detail.authSetting['scope.writePhotosAlbum']) {
      that.setData({
        isShow: false
      })
    } else {
      that.setData({
        isShow: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
    console.log()

    var img = this.data.shareurl //  分享图片
    return {
      title: '慧复健康', //转发的标题
      path: '/pages/index/index?yq_uqid=' + wx.getStorageSync('uqid'),
      imageUrl: img,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    } //转发的图片
  }
  
})